# -*- coding:utf-8 -*-

# @Time: 2025/3/16 16:22

# @Author: zhangbo

# @File: OverThermalPage.py

# @version: v1.0

import os
import os.path
import time

from selenium.webdriver.common.by import By

from base.OverThermalBase import OverThermalBase
from common.ObjectMap import ObjectMap
from utils.update_conf_file import UpdateConfFile
from utils.start_exe import StartExe


class OverThermalPage(OverThermalBase):

    def click_alarm_center(self, driver):
        """
        点击报警中心
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.alarm_center_li())

    def click_alarm_rules(self, driver):
        """
        点击报警规则
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.alarm_rules_a())

    def click_over_thermal_rules(self, driver):
        """
        点击超温报警
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.over_thermal_rules_li())

    def click_add_over_thermal_rules(self, driver):
        """
        点击添加超温报警规则
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.add_over_thermal())

    def click_enable_button(self, driver):
        """
        点击启用
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.enable_over_thermal())

    def input_alarm_name(self, driver):
        """
        输入报警规则名称
        :param driver:
        :return:
        """
        alarm_name = "自动化用例超温" + str(time.time())
        return ObjectMap().element_fill_value(driver, By.XPATH, self.alarm_name(), alarm_name)

    def setting_one_temperature_requirements(self, driver):
        """
        设置一路温度需求
        :param driver:
        :return:
        """

        return (ObjectMap().element_fill_value(driver, By.XPATH, self.one_temperature_requirements()[0], 80),
                ObjectMap().element_fill_value(driver, By.XPATH, self.one_temperature_requirements()[1], 10),
                ObjectMap().element_fill_value(driver, By.XPATH, self.one_temperature_requirements()[2], 10))

    def last_click_save_button(self, driver):
        """
        最后点击保存按钮
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.click_save_button())

    def update_conf_param(self):
        """
        更新conf文件
        :return:
        """
        try:
            # 接收要修改参数
            param_list = self.get_simulator_conf_parameters()

            # 修改 SimulatorConf 配置参数
            UpdateConfFile().update_conf_file(source_file_path=param_list[5], param_list=param_list)

        except Exception as e:
            print("修改conf文件失败，修改失败原因：%s" % e)

    def update_gps_info(self, source_path=None):
        """
        配置gps_info文件信息
        :param source_path: 配置的源文件
        :return:
        """
        try:
            # 更新gps_info文件
            UpdateConfFile().update_gps_info(source_path, self.get_simulator_conf_parameters()[6])
        except Exception as e:
            print("修改gps_info文件失败，修改失败原因：%s" % e)

    def start_exe_file(self, sleep_time=60):
        """
        启动exe文件
        :sleep_time: 模拟器持续时间
        :return:
        """

        try:
            start_time, end_time = StartExe().start_exe_file(exe_path=self.get_exe_path(), sleep_time=sleep_time)
            return start_time, end_time
        except Exception as e:
            print("启动exe文件失败，启动失败原因：%s" % e)

    def click_alarm_ledger(self, driver):
        """
        点击报警台账
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.get_alarm_center())

    def click_alarm_ledger_clear(self, driver):
        """
        点击报警条件清除
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.get_alarm_ledger_clear_button())

    def send_get_alarm_type(self, driver):
        """
        输入报警类型
        :param driver:
        :return:
        """
        return ObjectMap().element_fill_value(driver, By.XPATH, self.get_alarm_type(), "超高温报警")

    def click_get_choose_over_thermal(self, driver):
        """
        点击选择报警类型
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.get_choose_over_thermal())

    def send_car_num_input(self, driver):
        """
        输入车牌号，并选择
        :return:
        """
        car_number = self.car_id()
        return (ObjectMap().element_fill_value(driver, By.XPATH, self.get_car_num_input()[0], car_number),
                ObjectMap().element_click(driver, By.XPATH, self.get_car_num_input()[1]))

    def send_start_end_time(self, driver, start_time, end_time):
        """
        输入开始结束时间
        :param driver:
        :return:
        """
        return (ObjectMap().element_fill_value(driver, By.XPATH, self.get_start_end_time()[0], start_time),
                ObjectMap().element_fill_value(driver, By.XPATH, self.get_start_end_time()[1], end_time))

    def click_query_button(self, driver):
        """
        点击查询按钮
        :param driver:
        :return:
        """

        return ObjectMap().element_click(driver, By.XPATH, self.query_button())

    def alarm_assert_result(self, driver):
        """
        报警上报校验
        :param driver:
        :return:
        """
        try:
            driver.find_element(By.XPATH, self.result_element_tr())

            return True, "平台接收到对应的禁止驶入报警"
        except Exception as e:
            return False, "平台没有接收到报警项！请定位原因"
